﻿Imports System.Windows.Forms
Imports System.Data
Imports System.Data.OracleClient
Imports Barra

Public Class Frm_Piloto
    Dim VectorDeDatos(5) As TextBox
    'conexion para inicializar la barra del navegador
    Dim conexion As String = "Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes;Pwd=redes;"

    'query para el dgv
    Dim querydgv As String = " select id_piloto_empleado as Piloto, nombre as Nombre, direccion as Domicilio, telefono As Tel, No_licencia as Licencia, tipo_licencia as Tipo  from TB_LOT_PILOTO_EMPLEADO order by id_piloto_empleado"


    'llenado del datagridview
    'DgvVehiculo.DataSource=llenarDGV(querydgv)
    'DgvEntrega.DataSource = LlenarDgv(querydgv)

    'variable para llevar la secuencia en el campo ID
    Dim querysecuencia As String = "select max(ID_PILOTO_EMPLEADO)+ 1 as ID_PILOTO_EMPLEADO from TB_LOT_PILOTO_EMPLEADO"
    Dim camposecuencia As String = "ID_PILOTO_EMPLEADO"
    Public Function LlenarVector() As TextBox()
        VectorDeDatos(0) = TXTPILOTO
        VectorDeDatos(1) = TXTNOMBRE
        VectorDeDatos(2) = TXTDIR
        VectorDeDatos(3) = TXTTEL
        VectorDeDatos(4) = TXTNL
        VectorDeDatos(5) = TXTTL

        Return VectorDeDatos
    End Function
    Private Sub Frm_Piloto_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        VectorDeDatos = LlenarVector()
        BarraNavegacion1.Inicializar("Driver={Microsoft ODBC for Oracle};Server=xe;Uid=redes; Pwd=redes;", "TB_LOT_PILOTO_EMPLEADO", VectorDeDatos)
        'para llevar la secuencia del codigo
        'llenado del datagridview
        DGVPIL.DataSource = funciones.LlenarDgv(querydgv)
        Dim temp As String = funciones.traersecuencia(querysecuencia, camposecuencia)
        'puse esto de funciones.traersecuencia porque sino me lo tiraba como error entonces llame la clase general primero
        If temp = "" Then
            TXTPILOTO.Text = "1"
        Else
            TXTPILOTO.Text = temp
        End If
        Dim Cmb_tiplic() As String = {"A-Profesional", "B-SemiProfesional", "C-Particular", "M-Motocicleta"}
        cmbtipolic.Items.AddRange(Cmb_tiplic)
    End Sub
  
    Private Sub TXTPILOTO_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TXTPILOTO.TextChanged
        Dim temp As String = funciones.traersecuencia(querysecuencia, camposecuencia)
        If temp = "" Then
            TXTPILOTO.Text = "1"
        Else
            TXTPILOTO.Text = temp
        End If
    End Sub

    Private Sub cmbtipolic_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbtipolic.SelectedIndexChanged
        TXTTL.Text = cmbtipolic.SelectedItem
    End Sub
End Class
Public Class funciones

    Public Shared Function LlenarDgv(ByVal querydgv As String) As DataTable
        Dim conexion2 As String = "Server=xe;Uid=redes;Pwd=redes;"
        'Creamos la conexion
        Dim oracon As New OracleConnection(conexion2)
        'Abrimos la conexion
        oracon.Open()
        'Creamos un comando
        Dim oraCommand As OracleCommand = New OracleCommand
        'Establecemos la conexion para el comando
        oraCommand.Connection = oracon
        'Escribimos el texto del comando
        oraCommand.CommandText = querydgv
        'Creamos un objeto adaptador
        Dim oraDataAdapter As New OracleDataAdapter
        oraDataAdapter.SelectCommand = oraCommand
        'Llenamos la tabla
        Dim dttabla As New DataTable
        oraDataAdapter.Fill(dttabla)

        Return dttabla


        oracon.Close()

    End Function

    Public Shared Sub llenarcombo(ByRef combo As System.Windows.Forms.ComboBox, ByVal query As String, ByVal valor As String, ByVal display As String) 'As DataTable
        Dim conexion2 As String = "Server=xe;Uid=redes;Pwd=redes;"
        'Creamos la conexion
        Dim oraCon As New OracleConnection(conexion2)
        'Abrimos la conexion
        oraCon.Open()
        'Creamos un comando
        Dim oraCommand As OracleCommand = New OracleCommand
        'Establecemos la conexion para el comando
        oraCommand.Connection = oraCon
        'Escribimos el texto del comando
        oraCommand.CommandText = query
        'Creamos un objeto adaptador
        Dim oraDataAdapter As New OracleDataAdapter
        oraDataAdapter.SelectCommand = oraCommand
        'Llenamos la tabla
        Dim dttabla As New DataTable
        oraDataAdapter.Fill(dttabla)
        combo.DataSource = dttabla
        'Establecemos el valor a mostrar y el valor a insertar
        combo.ValueMember = valor
        combo.DisplayMember = display
        'Return dttabla
    End Sub

    Public Shared Function traersecuencia(ByVal querysecuencia As String, ByVal campo As String) As String
        Dim secuencia As String
        Dim iColumna As Integer

        Dim conexion2 As String = "Server=xe;Uid=redes;Pwd=redes;"
        'Creamos la conexion
        Dim oraCon As New OracleConnection(conexion2)
        'Abrimos la conexion
        oraCon.Open()
        'Creamos un comando
        Dim oraCommand As OracleCommand = New OracleCommand
        'Establecemos la conexion para el comando
        oraCommand.Connection = oraCon
        'Escribimos el texto del comando
        oraCommand.CommandText = querysecuencia
        Dim oraDataAdapter As New OracleDataAdapter
        oraDataAdapter.SelectCommand = oraCommand
        'Llenamos la tabla
        Dim dtData As New DataTable
        oraDataAdapter.Fill(dtData)
        Try
            iColumna = dtData.Columns(campo).Ordinal
            secuencia = dtData.Rows(0).ItemArray(iColumna).ToString()
        Catch ex As Exception
            ' secuencia = ""
        End Try
        Return secuencia
    End Function
End Class